package cn.action;

import cn.entity.User;
import cn.service.UserService;
import cn.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class UserAction extends ActionSupport {

    private String name;
    private String password;
    private User user;
    private User registerUser;
    private String nextDispose;

    public String login(){
        user = new User();
        user = userService.login(name,password);
        HttpServletRequest request = ServletActionContext.getRequest();
        if (user==null){
            addFieldError("user","用户不存在");
            return INPUT;
        }else if (!user.getPassword().equals(password)){
            addFieldError("pwd","密码错误");
            return INPUT;
        }else {
            request.getSession().setAttribute("user",user);
            return "list";
        }
    }

    public String register(){
        Integer id = userService.register(registerUser);
        HttpServletRequest request = ServletActionContext.getRequest();
        if (0!=id){
            request.setAttribute("msg","注册成功");
            return LOGIN;
        }else {
            addFieldError("msg","注册失败");
            return INPUT;
        }
    }



    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private UserService userService = new UserServiceImpl();

    public String getNextDispose() {
        return nextDispose;
    }

    public void setNextDispose(String nextDispose) {
        this.nextDispose = nextDispose;
    }

    public User getRegisterUser() {
        return registerUser;
    }

    public void setRegisterUser(User registerUser) {
        this.registerUser = registerUser;
    }


}
